CMAKE_MINIMUM_REQUIRED(VERSION 3.10)

MESSAGE("############# nim demo installer #############")

SET(CMAKE_CXX_STANDARD 11)
SET(TARGET_NAME installer)

IF (NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build." FORCE)
    MESSAGE(STATUS "No CMAKE_BUILD_TYPE selected, defaulting to ${CMAKE_BUILD_TYPE}")
ENDIF ()

IF (CMAKE_BUILD_TYPE MATCHES "Release")
    ADD_DEFINITIONS(-DNDEBUG)
ENDIF ()

SET(CONAN_DISABLE_CHECK_COMPILER ON)
SET(CMAKE_INCLUDE_CURRENT_DIR ON)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_LIST_DIR}/install/bin")
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_LIST_DIR}/install/bin")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Gm- /Zi /MT")
SET(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG /OPT:REF /OPT:ICF")
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON)
SET_PROPERTY(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "cmake")
ADD_COMPILE_OPTIONS("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
ADD_COMPILE_OPTIONS("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")

# Custom vars
SET(GROUP_BASE "base")
SET(GROUP_UI_COMPONENTS "ui_component")
SET(RELEASE_OUTPUT_7Z_FILE ${CMAKE_BINARY_DIR}/release.7z)
STRING(REPLACE "/" "\\\\" RELEASE_OUTPUT_7Z_FILE_WIN ${RELEASE_OUTPUT_7Z_FILE})
SET(RESOURCE_OUTPUT_ZIP_FILE ${CMAKE_CURRENT_SOURCE_DIR}/install/resources.zip)
STRING(REPLACE "/" "\\\\" RESOURCE_OUTPUT_ZIP_FILE_WIN ${RESOURCE_OUTPUT_ZIP_FILE})

PROJECT(${TARGET_NAME} DESCRIPTION "NetEase IM Demo Installer")

ADD_DEFINITIONS(
    -DUNICODE 
    -D_UNICODE 
    -DPSAPI_VERSION=1
)

# Git information
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/.cmake/")
INCLUDE(GetGitRevisionDescription)
git_release_version(GIT_TAG)
IF (GIT_TAG STREQUAL "")
    git_latest_tag(GIT_TAG)
ENDIF ()
git_describe(GIT_DESCRIBE)
git_commit_counts(GIT_COMMIT_COUNT)
STRING(REPLACE "." "," GIT_TAG_WITH_COMMA ${GIT_TAG})
MESSAGE(STATUS "Current git tag: ${GIT_TAG}, commit count: ${GIT_COMMIT_COUNT}, describe: ${GIT_DESCRIBE}")

# Depended third_party
ADD_SUBDIRECTORY(base)
ADD_SUBDIRECTORY(duilib)
ADD_SUBDIRECTORY(shared)
# ADD_SUBDIRECTORY(ui_component/ui_kit)

INCLUDE_DIRECTORIES(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/install/
    ${CMAKE_CURRENT_SOURCE_DIR}/build/
)

CONFIGURE_FILE(
	${CMAKE_CURRENT_SOURCE_DIR}/install/setup.rc.in
	${CMAKE_BINARY_DIR}/setup.rc
)

# pack release.7z for setup resource
ADD_CUSTOM_TARGET(pack_binaries
    COMMAND ${CMAKE_COMMAND} -E tar "cf" "${RELEASE_OUTPUT_7Z_FILE}" --format=7zip -- ${CMAKE_CURRENT_SOURCE_DIR}/../bin
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../
    VERBATIM
    USES_TERMINAL
    COMMENT "Packing release 7z files....... ${CMAKE_CURRENT_SOURCE_DIR}/../bin"
)
SET_TARGET_PROPERTIES(pack_binaries PROPERTIES FOLDER "setup")

# pack resource.zip for setup resource
ADD_CUSTOM_TARGET(pack_install_res ALL
	COMMAND ${CMAKE_COMMAND} -E tar "cf" "${RESOURCE_OUTPUT_ZIP_FILE}" --format=zip -- ${CMAKE_CURRENT_SOURCE_DIR}/install/bin/resources
	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/install/bin/
    VERBATIM
    USES_TERMINAL
    COMMENT "Packing resources files....... ${CMAKE_CURRENT_SOURCE_DIR}/install/bin/resources"
)
SET_TARGET_PROPERTIES(pack_install_res PROPERTIES FOLDER "setup")

FILE(GLOB_RECURSE NIM_SETUP_SOURCE install/*.cc install/*.h install/*.cpp install/*.c)
ADD_EXECUTABLE(
    ${TARGET_NAME}
    EXCLUDE_FROM_ALL
    ${NIM_SETUP_SOURCE}
    ${CMAKE_BINARY_DIR}/setup.rc
)

# set dependencies
ADD_DEPENDENCIES(${TARGET_NAME} pack_install_res pack_binaries)

TARGET_LINK_LIBRARIES(${TARGET_NAME}
    base
    duilib
    shared
    DbgHelp
    Version
    Netapi32
    Snmpapi
    Userenv
    Ws2_32
    Psapi
    Iphlpapi
    Imm32
    Comctl32
    User32
    Vfw32
    gdiplus
    Msimg32
)

SET_TARGET_PROPERTIES(${TARGET_NAME} PROPERTIES
    FOLDER "setup"
    LINK_FLAGS "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\" /SUBSYSTEM:WINDOWS"
	OUTPUT_NAME "NIM_Demo_Setup_${GIT_TAG}.${GIT_COMMIT_COUNT}"
)
